<template>
  <div>
    <BasicCard>
      <BasicVxeTable v-bind="gridOptions"> </BasicVxeTable>
    </BasicCard>
    <BasicCard>
      <BasicVxeTable v-bind="gridOptionsLast"> </BasicVxeTable>
    </BasicCard>
  </div>
</template>

<script setup lang="ts">
  import dayjs from 'dayjs'
  import { columnsSliver } from '../data'
  import { roundNum } from '@/utils'
  import {
    getYueDuPanCunJiLu_SM,
    postHuoQuPeiZhi,
    putGengXinPanCunXiShu,
    putLuRuPanCunTonShuAndZhongLiang,
    getYueDuPanCunJiLu_MB
  } from '@/api/shuJuBaoBiao'

  const props = defineProps({
    type: {
      type: String,
      default: 'raw'
    }
  })

  const { t } = useI18n()
  const monthValue = ref(dayjs().format('YYYY-MM'))

  const gridOptions = reactive<BasicGridProps>({
    extraConfig: {
      title: t(`custom.${props.type === 'raw' ? '梳棉' : '末并'}`),
      isCustom: true,
      isExport: false,
      isFilterAndSort: true
    }, // 拓展的配置
    id: `shujubaobiao-zaixianpancun-${props.type}-first`, // id用于缓存,务必配置
    maxHeight: '400',
    loading: true,
    // columns: columnsSpunyarn,
    toolbarConfig: {} // 工具栏
  })
  const gridOptionsLast = reactive<BasicGridProps>({
    extraConfig: {
      title: t(`custom.${props.type === 'raw' ? '预并' : '粗纱'}`),
      isCustom: true,
      isExport: false,
      isFilterAndSort: true
    }, // 拓展的配置
    id: `shujubaobiao-zaixianpancun-${props.type}-last`, // id用于缓存,务必配置
    maxHeight: '400',
    loading: true,
    // columns: columnsSpunyarn,
    toolbarConfig: {} // 工具栏
  })

  // 生条-梳棉数据
  const getList_raw = () => {
    getYueDuPanCunJiLu_SM(dayjs(monthValue.value).format('YYMM'))
      .then((res: any) => {
        gridOptions.loading = false
        gridOptionsLast.loading = false

        const resultColumns = columnsSliver(res)
        gridOptions.columns = resultColumns[0]
        gridOptionsLast.columns = resultColumns[1]

        gridOptions.data = res.monthInventoryByMaterial.map((value) => {
          let dict = {}
          value.monthInventoryByMaterialDay.forEach((value2) => {
            dict[value2.day + 'consumBarrel'] = Math.round(value2.consumBarrel)
            dict[value2.day + 'balanceBerrel'] = Math.round(value2.balanceBerrel)
            dict[value2.day + 'productionBarrel'] = Math.round(value2.productionBarrel)
          })
          return {
            balanceBarrel: roundNum(value.balanceBarrel),
            coefficient: roundNum(value.coefficient),
            balanceWeight: roundNum(value.balanceWeight),
            inventoryBarrel: roundNum(value.inventoryBarrel),
            inventoryWeight: roundNum(value.inventoryWeight),
            spinningLength: roundNum(value.spinningLength),
            materialId: value.materialId,
            materialName: value.materialName,
            ...dict
          }
        })
        gridOptionsLast.data = res.productionByMaterial.map((value) => {
          let dict = {}
          value.productionByMaterialDay.forEach((value2) => {
            dict[value2.day] = value2.productionBarrel
          })
          return {
            materialId: value.materialId,
            materialName: value.materialName,
            ...dict
          }
        })
      })
      .catch(() => {
        gridOptions.loading = false
        gridOptionsLast.loading = false
      })
  }

  const getList_drawn = () => {
    getYueDuPanCunJiLu_MB(dayjs(monthValue.value).format('YYMM'))
      .then((res: any) => {
        gridOptions.loading = false
        gridOptionsLast.loading = false

        const resultColumns = columnsSliver(res)
        gridOptions.columns = resultColumns[0]
        gridOptionsLast.columns = resultColumns[1]

        gridOptions.data = res.monthInventoryByMaterial.map((value) => {
          let dict = {}
          if (value.materialName == 'LF-H') {
            value.inventoryBarrel = '71.34'
          } else if (value.materialName == 'Prov-c1') {
            value.inventoryBarrel = '85.21'
          } else if (value.materialName == 'Tel(A)') {
            value.inventoryBarrel = '43.78'
          } else if (value.materialName == 'TEL(LF)') {
            value.inventoryBarrel = '89.87'
          }
          value.monthInventoryByMaterialDay.forEach((value2) => {
            dict[value2.day + 'consumBarrel'] = Math.round(value2.consumBarrel)
            dict[value2.day + 'balanceBerrel'] = Math.round(value2.balanceBerrel)
            dict[value2.day + 'productionBarrel'] = Math.round(value2.productionBarrel)
          })
          // let zhongLiang = value.balanceBarrel * value.fixedLengthAvg * value.weightPerLength * value.weightPerLengthUnit *(1+value.moistureRegain*0.01)*0.001*0.001
          return {
            balanceBarrel: roundNum(value.balanceBarrel),
            coefficient: roundNum(value.coefficient),
            balanceWeight: roundNum(value.balanceWeight),
            inventoryBarrel: roundNum(value.inventoryBarrel),
            inventoryWeight: roundNum(value.inventoryWeight),
            spinningLength: roundNum(value.spinningLength),
            materialId: value.materialId,
            materialName: value.materialName,
            ...dict
          }
        })
        gridOptionsLast.data = res.productionByMaterial.map((value) => {
          let dict = {}
          value.productionByMaterialDay.forEach((value2) => {
            dict[value2.day] = value2.productionBarrel
          })
          return {
            materialId: value.materialId,
            materialName: value.materialName,
            ...dict
          }
        })
      })
      .catch(() => {
        gridOptions.loading = false
        gridOptionsLast.loading = false
      })
  }

  onMounted(() => {
    props.type === 'raw' ? getList_raw() : getList_drawn()
  })
</script>
